<%@ Page language="vb" CodeFile="addInvoice.aspx.vb" Inherits="addInvoice_aspx_vb" %>

<%
                           rowNumber = 1
    invoiceID = Request.QueryString("invoiceID")
    If Len(invoiceID) > 0 Then 
        invoiceID = CLng(invoiceID)
    End IF
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        If Len(invoiceID) > 0 Then 
            dbOpen()
            RecSet = Conn.Execute("Select i.paymentTerm, p.partnerName From (tblInvoice i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) Where invoiceID = " & invoiceID & "")
            If RecSet.EOF Then 
                Response.Redirect("addInvoice.aspx")
            Else
                paymentTerm = RecSet.Fields("paymentTerm").Value
                partnerName = RecSet.Fields("partnerName").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""invoiceDateYear"", ""<1""],"
        strJS = strJS + "[""invoiceDateMonth"", ""<1""],"
        strJS = strJS + "[""invoiceDateDay"", ""<1""]"
        checkFields()
        realTime = Now()
        year = Left(realTime, 4)
        month = Mid(realTime, 6, 2)
        day = Mid(realTime, 9, 2)
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>ACTIVATE AN INVOICE<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='addInvoice.aspx?do=save&invoiceID=" & invoiceID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Invoice ID:</td><td align='right'><input type='text' name='invoiceID' id='invoiceID' value='" & invoiceID & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Partner:</td><td align='right'><input type='text' name='partnerName' id='partnerName' value='" & partnerName & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Payment term (days):</td><td align='right'><input type='text' name='paymentTerm' id='paymentTerm' value='" & paymentTerm & "' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Invoice date:&nbsp; </td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='invoiceDateYear' id='invoiceDateYear'>")
        Response.Write("<option value=''>&nbsp; Year ")
        stopYear = CDbl(year) + 3
        yearCounter = year
        For yearLoop = year To stopYear
            Response.Write("<option value='" & yearCounter & "'")
            If CStr(yearCounter) = year Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateMonth' id='invoiceDateMonth'>")
        Response.Write("<option value=''>&nbsp; Month ")
        yearCounter = 1
        For yearLoop = 1 To 12
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(month) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateDay' id='invoiceDateDay'>")
        Response.Write("<option value=''>&nbsp; Day&nbsp; ")
        yearCounter = 1
        For yearLoop = 1 To 31
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(day) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td>")
        Response.Write("<div id='error_invoiceDateYear' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateMonth' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateDay' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("</td>")
        Response.Write("</tr>")
        If Len(invoiceID) > 0 Then 
            Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Save'></td></tr>")
        End IF
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='4'>QUEUED INVOICES<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceID, p.partnerName, c.country From ((tblInvoice i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) Where i.invoiceAmount = 0 AND i.invoicePrinted = 0")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>No invoices found.</font></td></tr>")
        Else
            Response.Write("<tr bgcolor='#eeeeee'>")
            Response.Write("<td class='formBold'>Invoice ID</td>")
            Response.Write("<td class='formBold'>Partner</td>")
            Response.Write("<td class='formBold'>Country</td>")
            Response.Write("<td class='formBold'></td>")
            Response.Write("</tr>")
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td algin='right'>" & partnerName & "</td>")
                Response.Write("<td algin='right'>" & country & "</td>")
                Response.Write("<td algin='right'><a href='addInvoice.aspx?invoiceID=" & invoiceID & "'>activate</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='4'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        invoiceDateYear = Request.Form("invoiceDateYear")
        invoiceDateMonth = Request.Form("invoiceDateMonth")
        invoiceDateDay = Request.Form("invoiceDateDay")
        paymentTerm = Request.Form("paymentTerm")
        invoiceDateYear = CLng(invoiceDateYear)
        invoiceDateMonth = CLng(invoiceDateMonth)
        invoiceDateDay = CLng(invoiceDateDay)
        paymentTerm = CLng(paymentTerm)
        If CLng(invoiceDateMonth) < 10 Then 
            invoiceDateMonth = "0" & invoiceDateMonth & ""
        End IF
        If CLng(invoiceDateDay) < 10 Then 
            invoiceDateDay = "0" & invoiceDateDay & ""
        End IF
        invoiceDate = invoiceDateYear & "-" & invoiceDateMonth & "-" & invoiceDateDay
        expiryDate = DateAdd("d", CDbl(paymentTerm), Convert.ToDateTime(invoiceDate))
        dbOpen()
        RecSet = Conn.Execute("Select SUM(p.price) As priceSum From (tblInvoiceContent ic INNER JOIN tblPriceList p ON p.priceListID = ic.priceListID) Where ic.invoiceID = " & invoiceID & "")
        If RecSet.EOF Then 
            Response.Redirect("addInvoice.aspx")
        Else
            priceSum = RecSet.Fields("priceSum").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(priceSum) Then 
            priceSum = 0
        End IF
        Response.Write("priceSum = " & priceSum & "<br>")
        dbOpen()
        RecSet = Conn.Execute("Select SUM(ap.price) As priceAdditionalSum From (tblInvoiceContentAdditional ica INNER JOIN tblAdditionalProduct ap ON ap.additionalProductID = ica.additionalProductID) Where ica.invoiceID = " & invoiceID & "")
        If RecSet.EOF Then 
            Response.Redirect("addInvoice.aspx")
        Else
            priceAdditionalSum = RecSet.Fields("priceAdditionalSum").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(priceAdditionalSum) Then 
            priceAdditionalSum = 0
        End IF
        Response.Write("priceAdditionalSum = " & priceAdditionalSum & "<br>")
        invoiceAmount = priceSum + priceAdditionalSum
        invoiceAmount = Replace(CStr(invoiceAmount), ",", ".")
        invoiceClaim = invoiceAmount
        userID = fragusLeasingUserID
        Response.Write("userID = " & userID & "<br>")
        Response.Write("invoiceAmount = " & invoiceAmount & "<br>")
        Response.Write("invoiceClaim = " & invoiceClaim & "<br>")
        Response.Write("invoiceDate = " & invoiceDate & "<br>")
        Response.Write("expiryDate = " & expiryDate & "<br>")
        Response.Write("paymentTerm = " & paymentTerm & "<br>")
        dbOpen()
        RecSet = Conn.Execute("Select i.countryID, i.partnerID, co.decimals, co.countryCurrency From (tblInvoice i INNER JOIN tblCountry co ON co.countryID = i.countryID) Where i.invoiceID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            countryID = RecSet.Fields("countryID").Value
            partnerID = RecSet.Fields("partnerID").Value
            decimals = RecSet.Fields("decimals").Value
            countryCurrency = RecSet.Fields("countryCurrency").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If Convert.ToString(invoiceAmount) > 0 Then
            dbOpen()
            Response.Write("Update tblInvoice Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoiceID = " & invoiceID & "<br>")
            Conn.Execute("Update tblInvoice Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoiceID = " & invoiceID & "")
            dbClose()
            dbOpen()
            Response.Write("Insert Into tblInvoice (countryID, partnerID) Values(" & countryID & "," & partnerID & ")<br>")
            Conn.Execute("Insert Into tblInvoice (countryID, partnerID) Values(" & countryID & "," & partnerID & ")")
            dbClose()
            dbOpen()
            RecSet = Conn.Execute("SELECT MAX(invoiceID) AS invoiceID FROM tblInvoice")
            maxInvoiceID = RecSet.Fields("invoiceID").Value
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            dbOpen()
            RecSet = Conn.Execute("Select v.vehicleID, v.priceListID, (Select Max(ic.leaseCurrentDate) From tblInvoiceContent ic Where ic.vehicleID = v.vehicleID) As leaseCurrentDate From (tblInvoiceContent ic INNER JOIN tblVehicle v ON v.vehicleID = ic.vehicleID) Where v.completed = 0 AND v.active = 1 And ic.invoiceID = " & invoiceID & " Group By v.vehicleID, v.priceListID")
            If Not RecSet.EOF Then
                Do Until RecSet.EOF
                    vehicleID = RecSet.Fields("vehicleID").Value
                    priceListID = RecSet.Fields("priceListID").Value
                    leaseCurrentDate = RecSet.Fields("leaseCurrentDate").Value
                    leaseCurrentDate = DateAdd("m", 1, Convert.ToDateTime(leaseCurrentDate))
                    Response.Write("Insert Into tblInvoiceContent (invoiceID, vehicleID, priceListID, leaseCurrentDate) Values(" & maxInvoiceID & "," & vehicleID & "," & priceListID & ",'" & leaseCurrentDate & "')<br>")
                    Conn.Execute("Insert Into tblInvoiceContent (invoiceID, vehicleID, priceListID, leaseCurrentDate) Values(" & maxInvoiceID & "," & vehicleID & "," & priceListID & ",'" & leaseCurrentDate & "')")
                    RecSet.MoveNext()
                Loop
            End If
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            dbOpen()
            RecSet = Conn.Execute("Select ica.additionalProductID, v.vehicleID, v.priceListID, (Select Max(ica.leaseCurrentDate) From tblInvoiceContentAdditional ica Where ica.vehicleID = v.vehicleID) As leaseCurrentDate From (tblInvoiceContentAdditional ica INNER JOIN tblVehicle v ON v.vehicleID = ica.vehicleID) Where v.completed = 0 AND v.active = 1 And ica.invoiceID = " & invoiceID & " Group By ica.additionalProductID, v.vehicleID, v.priceListID")
            If Not RecSet.EOF Then
                Do Until RecSet.EOF
                    additionalProductID = RecSet.Fields("additionalProductID").Value
                    vehicleID = RecSet.Fields("vehicleID").Value
                    leaseCurrentDate = RecSet.Fields("leaseCurrentDate").Value
                    leaseCurrentDate = DateAdd("m", 1, Convert.ToDateTime(leaseCurrentDate))
                    Response.Write("Insert Into tblInvoiceContentAdditional (invoiceID, vehicleID, additionalProductID, leaseCurrentDate) Values(" & maxInvoiceID & "," & vehicleID & "," & additionalProductID & ",'" & leaseCurrentDate & "')<br>")
                    Conn.Execute("Insert Into tblInvoiceContentAdditional (invoiceID, vehicleID, additionalProductID, leaseCurrentDate) Values(" & maxInvoiceID & "," & vehicleID & "," & additionalProductID & ",'" & leaseCurrentDate & "')")
                    RecSet.MoveNext()
                Loop
            End If
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        Else
            maxInvoiceID = invoiceID
            leaseCurrentDate = Now()
            leaseCurrentDate = Left(leaseCurrentDate, 8) & "01"
            leaseCurrentDate = DateAdd("m", -1, Convert.ToDateTime(leaseCurrentDate))
            Response.Write("leaseCurrentDate = " & leaseCurrentDate & "<br>")
        End If
        dbOpen()
        RecSet = Conn.Execute("Select v.vehicleID, v.priceListID, v.leaseStartDate From tblVehicle v Where v.partnerID = " & partnerID & " And v.completed = 0 AND v.active = 1 And v.vehicleID Not In (Select vehicleID From tblInvoiceContent Where invoiceID = " & invoiceID & ")")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                Response.Write("**********<br>")
                vehicleID = RecSet.Fields("vehicleID").Value
                priceListID = RecSet.Fields("priceListID").Value
                leaseStartDate = RecSet.Fields("leaseStartDate").Value
                Response.Write("leaseStartDate = " & leaseStartDate & "<br>")
                checkFirstDay = Right(leaseStartDate, 2)
                If checkFirstDay = "01" Then 
                    leaseFirstPeriodDate = leaseStartDate
                Else
                    leaseFirstPeriodDate = Left(leaseStartDate, 8)
                    leaseFirstPeriodDate = leaseFirstPeriodDate & "01"
                    leaseFirstPeriodDate = DateAdd("m", 1, Convert.ToDateTime(leaseFirstPeriodDate))
                End IF
                leaseStartDateDiff = DateDiff("m", Convert.ToDateTime(leaseFirstPeriodDate), Convert.ToDateTime(leaseCurrentDate))
                Response.Write("leaseStartDateDiff = " & leaseStartDateDiff & "<br>")
                Response.Write("vehicleID = " & vehicleID & "<br>")
                Response.Write("leaseFirstPeriodDate = " & leaseFirstPeriodDate & "<br>")
                leaseCurrentDateDateAdd = leaseCurrentDate
                If leaseStartDateDiff >= 0 Then 
                    For leaseStartDateDiffLoop = 0 To leaseStartDateDiff
                        Response.Write("<font color='green'>NEW - Insert Into tblInvoiceContent (invoiceID, vehicleID, priceListID, leaseCurrentDate) Values(" & maxInvoiceID & "," & vehicleID & "," & priceListID & ",'" & leaseCurrentDateDateAdd & "')</font><br>")
                        Conn.Execute("Insert Into tblInvoiceContent (invoiceID, vehicleID, priceListID, leaseCurrentDate) Values(" & maxInvoiceID & "," & vehicleID & "," & priceListID & ",'" & leaseCurrentDateDateAdd & "')")
                        leaseCurrentDateDateAdd = DateAdd("m", - 1, Convert.ToDateTime(leaseCurrentDateDateAdd))
                    Next
                    leaseCurrentDateDateAdd = leaseCurrentDate
                    RS = Conn.Execute("Select apvc.additionalProductID, ap.price As additionalPrice From (tblAdditionalProductVehicleContent apvc INNER JOIN tblAdditionalProduct ap ON ap.additionalProductID = apvc.additionalProductID) Where vehicleID = " & vehicleID & "")
                    If Not RS.EOF Then 
                        Do Until RS.EOF
                            'Dim price
                            'Dim additionalProductID
                            Dim additionalPrice
                            additionalPrice = RS.Field().value
                            additionalProductID = RS.Fields("additionalProductID").Value
                            price = RS.Fields("additionalPrice").Value
                            price = Replace(CStr(price), ",", ".")
                            If CInt(price) > 0 Then
                                For leaseStartDateDiffLoop = 0 To leaseStartDateDiff
                                    Response.Write("<font color='green'>NEW - Insert Into tblInvoiceContentAdditional (invoiceID, vehicleID, additionalProductID, leaseCurrentDate) Values(" & maxInvoiceID & "," & vehicleID & "," & additionalProductID & ",'" & leaseCurrentDateDateAdd & "')</font><br>")
                                    Conn.Execute("Insert Into tblInvoiceContentAdditional (invoiceID, vehicleID, additionalProductID, leaseCurrentDate) Values(" & maxInvoiceID & "," & vehicleID & "," & additionalProductID & ",'" & leaseCurrentDateDateAdd & "')")
                                    leaseCurrentDateDateAdd = DateAdd("m", -1, Convert.ToDateTime(leaseCurrentDateDateAdd))
                                Next
                            End If
                            RS.MoveNext()
                        Loop
                    End IF
                    RS.Close()
                    RS = Nothing
                Else
                    Response.Write("<font color='red'>NOT SAVED, vehicleID = " & vehicleID & "</font>")
                End IF
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        '**** Add credit invoice ****
        dbOpen()
        RecSet = Conn.Execute("Select Count(invoiceCreditID) As invoiceCreditCount From tblInvoiceCredit Where invoiceID = " & invoiceID & "")
        invoiceCreditCount = RecSet.Fields("invoiceCreditCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CDbl(invoiceCreditCount) > 0 Then 
            dbOpen()
            RecSet = Conn.Execute("Select SUM(p.price) As priceSum From ((tblInvoiceCredit ic INNER JOIN tblInvoiceCreditContent icc On icc.invoiceCreditID = ic.invoiceCreditID) INNER JOIN tblPriceList p ON p.priceListID = icc.priceListID) Where ic.invoiceID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                priceSum = RecSet.Fields("priceSum").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            If IsDBNull(priceSum) = true Then 
                priceSum = 0
            End IF
            dbOpen()
            RecSet = Conn.Execute("Select SUM(ap.price) As priceAdditionalSum From ((tblInvoiceCredit ic INNER JOIN tblInvoiceCreditContentAdditional icca On icca.invoiceCreditID = ic.invoiceCreditID) INNER JOIN tblAdditionalProduct ap ON ap.additionalProductID = icca.additionalProductID) Where ic.invoiceID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                priceAdditionalSum = RecSet.Fields("priceAdditionalSum").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            If IsDBNull(priceAdditionalSum) = true Then 
                priceAdditionalSum = 0
            End IF
            invoiceAmount = priceSum + priceAdditionalSum
            invoiceAmount = Replace(CStr(invoiceAmount), ",", ".")
            invoiceClaim = invoiceAmount
            dbOpen()
            Response.Write("Update tblInvoiceCredit Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoiceID = " & invoiceID & "<br>")
            Conn.Execute("Update tblInvoiceCredit Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoiceID = " & invoiceID & "")
            dbClose()
        End IF
        '**** Add Admin fee invoice ****
        dbOpen()
        RecSet = Conn.Execute("Select Count(vehicleID) As allowInvoiceAdminFeeCount From ((tblInvoiceContent ic INNER JOIN tblPriceList pl ON pl.priceListID = ic.priceListID) INNER JOIN tblProduct p ON p.productID = pl.productID) Where p.allowInvoiceAdminFee = 1 And ic.invoiceID = " & invoiceID & "")
        allowInvoiceAdminFeeCount = RecSet.Fields("allowInvoiceAdminFeeCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CDbl(allowInvoiceAdminFeeCount) > 0 Then 
            dbOpen()
            RecSet = Conn.Execute("Select SUM(p.adminFee) As invoiceAmount From (tblInvoiceContent ic INNER JOIN tblPriceList p ON p.priceListID = ic.priceListID) Where ic.invoiceID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            If IsDBNull(invoiceAmount) Then 
                invoiceAmount = 0
            End IF
            invoiceAmount = Replace(CStr(invoiceAmount), ",", ".")
            invoiceClaim = invoiceAmount
            dbOpen()
            Response.Write("Update tblInvoiceAdminFee Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoiceID = " & invoiceID & "<br>")
            Conn.Execute("Update tblInvoiceAdminFee Set userID = " & userID & ", invoiceAmount = " & invoiceAmount & ", invoiceClaim = " & invoiceClaim & ", invoiceDate = '" & invoiceDate & "', expiryDate = '" & expiryDate & "', paymentTerm = " & paymentTerm & " Where invoiceID = " & invoiceID & "")
            dbClose()
        End IF
        dbOpen()
        RecSet = Conn.Execute("Select Count(vehicleID) As allowInvoiceAdminFeeCount From ((tblInvoiceContent ic INNER JOIN tblPriceList pl ON pl.priceListID = ic.priceListID) INNER JOIN tblProduct p ON p.productID = pl.productID) Where p.allowInvoiceAdminFee = 1 And ic.invoiceID = " & maxInvoiceID & "")
        allowInvoiceAdminFeeCount = RecSet.Fields("allowInvoiceAdminFeeCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CDbl(allowInvoiceAdminFeeCount) > 0 Then 
            dbOpen()
            Response.Write("Insert Into tblInvoiceAdminFee (countryID, partnerID, invoiceID) Values(" & countryID & "," & partnerID & "," & maxInvoiceID & ")<br>")
            Conn.Execute("Insert Into tblInvoiceAdminFee (countryID, partnerID, invoiceID) Values(" & countryID & "," & partnerID & "," & maxInvoiceID & ")")
            dbClose()
        End IF
        Response.Redirect("addInvoice.aspx?subDo=%20-%20ACTIVATED")
    End IF
%>
